/**
 * Copyright © 2016-2025 The Thingsboard Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.terracloud.server.dao.model.sql;

import com.terracloud.server.common.data.device.DeviceAccessControlMode;
import com.terracloud.server.common.data.device.DeviceAccessControlRule;
import com.terracloud.server.common.data.device.DeviceAccessControlRuleType;
import com.terracloud.server.common.data.id.DeviceAccessControlRuleId;
import com.terracloud.server.common.data.id.TenantId;
import com.terracloud.server.dao.model.BaseSqlEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Table;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.UUID;

@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "device_access_control_rule")
public class DeviceAccessControlRuleEntity extends BaseSqlEntity<DeviceAccessControlRule> {

    @Column(name = "tenant_id")
    private UUID tenantId;

    @Enumerated(EnumType.STRING)
    @Column(name = "type")
    private DeviceAccessControlRuleType type;

    @Column(name = "value")
    private String value;

    @Column(name = "enabled")
    private boolean enabled;
    
    @Enumerated(EnumType.STRING)
    @Column(name = "mode")
    private DeviceAccessControlMode mode;
    
    @Column(name = "description")
    private String description;

    public DeviceAccessControlRuleEntity() {
        super();
        this.enabled = true;
    }

    public DeviceAccessControlRuleEntity(DeviceAccessControlRule deviceAccessControlRule) {
        if (deviceAccessControlRule.getId() != null) {
            this.setId(deviceAccessControlRule.getId().getId());
        }
        if (deviceAccessControlRule.getTenantId() != null) {
            this.tenantId = deviceAccessControlRule.getTenantId().getId();
        }
        this.type = deviceAccessControlRule.getType();
        this.value = deviceAccessControlRule.getValue();
        this.enabled = deviceAccessControlRule.isEnabled();
        this.mode = deviceAccessControlRule.getMode();
        this.description = deviceAccessControlRule.getDescription();
    }

    @Override
    public DeviceAccessControlRule toData() {
        DeviceAccessControlRule deviceAccessControlRule = new DeviceAccessControlRule(new DeviceAccessControlRuleId(this.getId()));
        deviceAccessControlRule.setCreatedTime(this.getCreatedTime());
        deviceAccessControlRule.setTenantId(TenantId.fromUUID(this.tenantId));
        deviceAccessControlRule.setType(this.type);
        deviceAccessControlRule.setValue(this.value);
        deviceAccessControlRule.setEnabled(this.enabled);
        deviceAccessControlRule.setMode(this.mode);
        deviceAccessControlRule.setDescription(this.description);
        return deviceAccessControlRule;
    }
}